/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
 *
 * @author hicham
 * This class represents a concept in an ontology
 */
public class Concept {
    
    static public int idLastCreated = 0;
    
    private int id;
    private String name;
    private List<Attribute> attributes = new ArrayList<Attribute>();
    // an ontology is a tree structure, so this concept may have parents he heritates attributes from and children 
    private List<Concept> parents = new ArrayList<Concept>(); 
    private List<Concept> children = new ArrayList<Concept>();
    private List<Relation> relations = new ArrayList<Relation>();

    public Concept() {
        super();
        this.id = Commun.IncrementId(idLastCreated,1);
        idLastCreated++;
    }

    public Concept(String name) {
        this();
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Attribute> getAttributes() {
        return attributes;
    }

    public void setAttributes(List<Attribute> attributes) {
        this.attributes = attributes;
    }
    
    public void addAttribute(Attribute a) {
        attributes.add(a);
    }
    
    /**
     * Add the attribute to this concept and all its descendants
     * @param a 
     */
    public void addAttributeRecursively(Attribute a) {
        attributes.add(a);
        for(Concept c : children) {
            c.addAttributeRecursively(a);
        }
    }

    public List<Concept> getChildren() {
        return children;
    }

    public void setChildren(List<Concept> children) {
        this.children = children;
    }
    
    public void addChild(Concept child) {
        children.add(child);
    }

    public List<Concept> getParents() {
        return parents;
    }

    public void setParents(List<Concept> parents) {
        this.parents = parents;
    }
    
    public void addParent(Concept parent) {
        parents.add(parent);
    }

    public List<Relation> getRelations() {
        return relations;
    }

    public void setRelations(List<Relation> relations) {
        this.relations = relations;
    }
    
    /* Top concept = no parent */
    public boolean isTopConcept() {
        return parents.isEmpty();
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 59 * hash + Objects.hashCode(this.name);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Concept other = (Concept) obj;
        if (!Objects.equals(this.name, other.name)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return name;
    }
    
    
}
